﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using BCNPCObject.DB;
using DbLinq;
using DbLinq.Oracle;
using CMISLibrary.Entities;
using CMISLibrary;

namespace BCNPCObject.EXCEL
{
    public class cls_DD_BCS_CTO_Controller
    {
        #region   Atributes

        //private DataSet CMIS_Header = new CMISOutputParameter();
        private DD_BCS_CTO info;

        #endregion

        #region   Constructor

        public cls_DD_BCS_CTO_Controller()
        {
            info = new DD_BCS_CTO();
        }
        public cls_DD_BCS_CTO_Controller(DD_BCS_CTO Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public DD_BCS_CTO pInfor
        {
            get { return info; }
            set { info = value; }
        }
        #endregion

        #region Methods
        public DataSet check_cto(string ma_dviqly, string so_cto, string ma_loai, string nam_sx)
        {
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            try
            {
                var x = from a in CMIS2.DB.DD_HOSO_CTO
                        where a.MA_DVIQLY == ma_dviqly && a.SO_CTO == so_cto && a.MA_CLOAI == ma_loai && a.NAM_SX == nam_sx // trang thai = 0 lấy khách hàng sinh hoạt
                        select new
                        {
                            a.MA_DVIQLY,
                            a.MA_CTO,
                            a.NAM_SX,
                            a.LOAI_SOHUU,
                            a.MA_BDONG,
                            a.MA_CLOAI,
                            a.NGAY_BDONG,
                        };
                if (x == null || x.Take(1).Count() <= 0)
                {
                    return null;
                }
                var y = from a in CMIS2.DB.DD_HOSO_CTO
                        where a.MA_DVIQLY == ma_dviqly && a.SO_CTO == so_cto && a.MA_CLOAI == ma_loai && a.NAM_SX == nam_sx && a.MA_BDONG =="B" // trang thai = 0 lấy khách hàng sinh hoạt
                        select a;
                if (y != null && y.Take(1).Count() > 0)
                {
                    return null;
                }
                dt = Utility.LINQToDataTable(x);
                ds.Tables.Add(dt);
                return ds;
            }
            catch (Exception ex)
            {
                //maloi = ex.ToString();
                CMIS2.ResetDB(); return null;
            }
        }
        public bool insert_DD_BCS_CTO(ref Int64 id_bcs,ref string maloi)
        {
            long _ID = CMIS2.DB.ExecuteCommand("Select SEQ_DD_BCS_CTO.NEXTVAL from dual");
            if (_ID == -1) return false;
            try
            {
                info.ID_BCS = Convert.ToInt64(_ID);
                DD_BCS_CTO objDD_BCS_CTO = new DD_BCS_CTO()
                {
                    MA_DVIQLY = info.MA_DVIQLY,
                    ID_BCS = info.ID_BCS,
                    MA_DDO = info.MA_DDO,
                    ID_BBAN = info.ID_BBAN,
                    MA_CTO = info.MA_CTO,
                    SO_CTO = info.SO_CTO,
                    BCS = info.BCS,
                    HS_NHAN = info.HS_NHAN,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_TAO = info.NGAY_TAO,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    NGUOI_SUA = info.NGUOI_SUA
                };
                CMIS2.DB.DD_BCS_CTO.InsertOnSubmit(info);
                CMIS2.DB.SubmitChanges();
                id_bcs = _ID;
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB();
                return false;
            }
        }
        public bool delete_DD_BCS_CTO(DataTable dt, ref string maloi)
        {
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        var query = from p in CMIS2.DB.DD_BCS_CTO
                                    where (p.MA_DVIQLY == dt.Rows[i]["MA_DVIQLY"].ToString() && p.ID_BCS == Convert.ToInt64(dt.Rows[i]["ID_BCS"]))
                                    select p;
                        if (query != null && query.Count() > 0)
                        {
                            CMIS2.DB.DD_BCS_CTO.DeleteAllOnSubmit(query.ToList());
                            CMIS2.DB.SubmitChanges();
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        #endregion
    }
}
